package light.mvc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {
	public static Date getNowTimeSegment() throws Exception{
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
		
		if(week == 0 || week == 6) {
			throw new Exception("非工作日");
		} 
		if(hour>=7 && hour<=11) {
			hour = 8;
		} else if(hour >=13 && hour <= 17) {
			hour = 14;
		} else {
			throw new Exception("非挂号时间段");
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date str2Date(String timeSegmentStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date timeSegment = null;
		try {
			timeSegment = format.parse(timeSegmentStr);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		return timeSegment;
	}
}
